<?php
namespace app\jincheng\controller;
use think\Log;
use think\Controller;
use think\Db;
use app\service\appletRoomService;
use app\service\xcxService;
class Room extends Controller{
	public function checkname(){
        $data=request()->post();
        //Log::write($data,"1234");
        $roominfo=Db::table("roominfo")
                    ->where("community_id",$data["community_id"])
                    ->where("zz_group",$data['group'])
                    ->where("building",$data['building'])
                    ->where("unit",$data['unit'])
                    ->where("room",$data['room'])
                    ->find();
        
        $userroom=Db::table("roominfo_user")->where("user_id",$data["user_id"])->where("room_id",$roominfo["id"])->find();
        if(empty($userroom)){
            return json(['errCode'=>'1',"room_id"=>$roominfo["id"]]);
        }else{
            return json(['errCode'=>'0',"room_id"=>$roominfo["id"]]);
        }
    }

    public function checkname_save(){
        $data=request()->post();
        $num=Db::table("roominfo_user")->where("user_id",$data["user_id"])->count();
        if($num>=3){
            return json(['errcode'=>'1','errmsg'=>"最多绑定三户"]); 
        }
        $roominfo=Db::table("roominfo")->where("id",$data["room_id"])->find();
        if($roominfo["owner_name"]==$data["owner_name"]){
            
            $info["user_id"]=$data["user_id"];
            $info["room_id"]=$data["room_id"];
            $info["community_id"]=$roominfo["community_id"];
            
            
            $info["address"]=$roominfo["address"];
            $info["create_time"]=time();
            $communityinfo=Db::table("community")->where("community_id",$roominfo["community_id"])->find();
            $info["community_name"]=$communityinfo["community_name"];
            $res=Db::table("roominfo_user")->insert($info);
            return json(['errcode'=>'0']);
        }else{
            return json(['errcode'=>'1','errmsg'=>"验证失败"]); 
        }
    }

    public function roominfo(){
        $room_id=request()->post("room_id");
        $roominfo=Db::table("roominfo")
                    ->where("id",$room_id)
                    ->find();
        $communityinfo=Db::table("community")->where("id",$roominfo["community_id"])->find();
        $roominfo["community_name"]=$communityinfo["name"];
        $roominfo["owner_name"]=str_repeat('*', mb_strlen($roominfo['owner_name'])-1).mb_substr($roominfo["owner_name"],-1);
        return json(['errCode'=>'0',"roominfo"=>$roominfo]);
    }
	public function address(){
		$user_id=request()->post("user_id");
		//$user_id="2088112618452297";
		$community_id=request()->post("adcode");
		$service_id=1;
		//查询小区
		
		$str="";
		$str=$str.'[';	
		///Log::write($community,"community");
		
		//查询组团
		$group=(new appletRoomService())->disgroup($community_id);
		foreach ($group as $gro){
			$str=$str.'{';
			$str=$str.'"name": "'.$gro.'",';
			$str=$str.'"subList": [';
			
			//查询房屋号
			$building=(new appletRoomService())->disbuild($community_id,$gro);
			
			foreach ($building as $build){
				$str=$str.'{';
                    $str=$str.'"name": "'.$build.'",';
                    $str=$str.'"subList": [';
                    //查询单元
                    $unit=(new appletRoomService())->disunit($community_id,$gro,$build);
                    foreach ($unit as $uni){
                    	$str=$str.'{';
                    		$str=$str.'"name": "'.$uni.'",';
                    		$str=$str.'"subList": [';
                    		//查询房屋号
                    		$room=(new appletRoomService())->disroom($community_id,$gro,$build,$uni);
                    		foreach ($room as $roo){
                    			$str=$str.'{';
                    				$str=$str.'"name": "'.$roo.'"';
                    			$str=$str.'},';
                    		}
                    		$str=substr($str,0,strlen($str)-1);
                    		$str=$str.']';
                    	$str=$str.'},';
                    }
                    $str=substr($str,0,strlen($str)-1);
                    $str=$str.']';
                $str=$str.'},';
			}
			$str=substr($str,0,strlen($str)-1);
			
			$str=$str.']';
			$str=$str.'},';
		}
				
		
		$str=substr($str,0,strlen($str)-1);
		$str=$str.']';
		return json_encode($str);
	}
	
	//小区列表
    public function  communityname(){
    	$serviceid=6;
        if(isset($_GET['communityname'])){
            $communityname = $_GET['communityname'];
            $li = (new appletRoomService())->communityName($communityname,$serviceid);

            foreach ($li as $l){
                $lis['adcode'] = $l['community_id'];
    			$lis['address'] = $l['community_address'];
                $lis['city'] = $l['community_name'];
                $list[] = $lis;
            }
        }else{
            $li = (new appletRoomService())->communityList($serviceid);
            //echo "<pre>";
            //var_dump($li);die;
            foreach ($li as $l){
                $lis['adcode'] = $l['community_id'];
    			$lis['address'] = $l['community_address'];
                $lis['city'] = $l['community_name'];
                $list[] = $lis;
            }
        }
        if(empty($list)){
            return json_encode($li);
        }else{
            return json_encode($list);
        }
    }
}